package com.yonyou.isv.demo.openapi.entity.output_invoice;

import com.google.gson.GsonBuilder;

import java.util.*;

/**
 * 销项开票构造参数
 * */
public class InvoiceBuildParam {

    /**
     * 未开票查询
     */
    public static Map<String, Object> result() {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        //发票代码
        paramsMap.put("lyid", "1");
        //发票号码
        paramsMap.put("djqqlsh", "23770399");
        paramsMap.put("orgCode", "20160914001");
        paramsMap.put("pageNum", "1");
        paramsMap.put("orgSize", "20160914001");

        return paramsMap;
    }


    /**
     * 构造 开票申请删除 表单数据
     */
    public static String buildInvoiceApplyDelParam() {
        List<Object> datas = new ArrayList<>();
        Map<String, String> requestdatas = new HashMap();
        requestdatas.put("FPQQLSH", "31642278241514833151");//发票请求流水号  必填
        requestdatas.put("XSF_NSRSBH", "123456789jgj");//销售方纳税人识别号 必填
        datas.add(requestdatas);
        return new GsonBuilder().create().toJson(datas);
    }


    /**
     * 开票申请审核通过
     */
    public static String issue() {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("FPQQLSH", "31642278241514833151");//必须为待开票的发票申请流水号
        paramsMap.put("XSF_NSRSBH", "123456789jgj");
        paramsMap.put("JSHJ", 117);
        datas.add(paramsMap);
        return new GsonBuilder().create().toJson(datas);
    }


    /**
     * url回掉配置
     */
    public static String buildUrlConfigs(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("fpqqlsh", fpqqlsh);
        data.put("url", "http://47x826661y.qicp.vip/kaipiao/callback1");
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }

    /**
     * 构造短信发送信息
     */
    public static String buildSmsConfigs(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("fpqqlsh", fpqqlsh);
        data.put("address", "155****3180");
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }

    /**
     * 构造email发送信息
     */
    public static String buildEmailConfigs(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("fpqqlsh", fpqqlsh);
        data.put("address", "jiaogjin@yonyou.com");
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }

    /**
     * 构造requestdatas
     */
    public static String buildRequestDatas(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("FPQQLSH", fpqqlsh);
        data.put("XSF_NSRSBH", "2343534345");
        //开票点档案-开票点编码
        data.put("ORGCODE", "5346645234");
        data.put("GMF_MC", "接口测试组织-请勿修改");
        data.put("GMF_NSRSBH", "111222333456111");
        //data.put("GMF_DZDH", "购买方地址、电话(数电票此字段非必填)");
        //data.put("GMF_YHZH", "购买方银行、账号(数电票此字段非必填)");
        //data.put("XSF_DZDH", "销售方地址、电话(数电票此字段非必填)");
        //data.put("XSF_YHZH", "销售方银行、账号(数电票此字段非必填)");
        data.put("GMF_YH", "购买方银行(数电专用字段)");
        data.put("GMF_ZH", "购买方账号(数电专用字段)");
        data.put("GMF_DZ", "购买方地址(数电专用字段)");
        data.put("GMF_DH", "购买方电话(数电专用字段)");
        data.put("XSF_YH", "销售方银行(数电专用字段)");
        data.put("XSF_ZH", "销售方账号(数电专用字段)");
        data.put("XSF_DZ", "销售方地址(数电专用字段)");
        data.put("XSF_DH", "销售方电话(数电专用字段)");
        //1、目前仅支持不需要验证码登录的地区实现接口自动开票，用户密码国密4加密，参考Sm4Test类
        //2、需要验证码登录的地区不用传用户名和密码。只能税务云后台保持登录状态，实现自动开票。
        //data.put("ALLELCUSERNAME", "数电发票用户名");
        //data.put("ALLELCPASSWORD", "数电发票密码");
        //31：数电专用发票；32：数电普通发票；1：增值税电子普通发票；2：增值税电子专用发票；3：增值税普通发票；
        data.put("FPLX", "32");
        data.put("JSHJ", 100);
        data.put("BZ", "如数电发票票面需要显示购方、销方的银行账号、地址电话。目前只能放到备注字段中，不超过400个字符。后期规划会在税务云系统增加配置参数【是否将此信息放到票面备注中】，勾选了就会自动将银行账号，地址电话显示到票面的备注中");
        data.put("LYID", "123456");
        data.put("items", buildItems());
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }

    /**
     * 构造redRequestdatas
     * @param fpqqlsh
     */
    public static String partRedRequestDatas(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("FPQQLSH",fpqqlsh );
        //开票点编码 如果一个税号对应多个开票点，此字段必输，用于确定唯一开票点
        //data.put("ORGCODE", "");
        //蓝色发票代码
        data.put("fpDm", "011002680026");
        //蓝色发票号码
        data.put("fpHm", "03197960");
        data.put("JSHJ", -2.06);
        data.put("items", buildRedItems());
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }

    private static Object buildRedItems() {
        List<Object> items = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("XMJSHJ", -2.06);
        data.put("XMMC", "稻谷");
        data.put("XMSL", -1);
        //税率3%需要写成0.03的格式
        data.put("SL", 0.03);
        //SPBM字段为商品税收分类编码，不同的商品会有不同的编码，不对应的话会影响报税，需要咨询下公司财务
        data.put("SPBM", "1090625010000000000");
        items.add(data);
        return items;
    }

    /**
     * 拆分
     *
     * @return
     */
    public static String buildRequestDatasSplit(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("FPQQLSH", fpqqlsh);
        data.put("XSF_NSRSBH", "123456789jgj");
        //开票点档案-开票点编码
        data.put("ORGCODE", "123456789");
        data.put("XSF_MC", "销售方名称");
        data.put("XSF_DZDH", "江苏省 无锡市   中国江苏省无锡市江阴市滨江西路");
        data.put("GMF_MC", "接口测试组织-请勿修改");
        data.put("GMF_MC", "接口测试组织-请勿修改");
        data.put("LYID", "12345678");
        data.put("JSHJ", 2000000);
        data.put("items", buildSplitItems());
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }

    private static Object buildSplitItems() {
        List<Object> items = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("XMMC", "POLO衫");//项目名称 必填
        data.put("XMJSHJ", 2000000);//项目价税合计 必填
        data.put("SL", 0.17);//税率 必填
        data.put("SPBM", "1090625010000000000");//商品的税收分类编码 必填
        items.add(data);
        return items;
    }

    /**
     * 构造request发票明细
     * 实际商品信息应和公司对应的编码信息相同
     */
    private static List<Object> buildItems() {
        List<Object> items = new ArrayList<Object>();
        Map<String, Object> data2 = new HashMap<String, Object>();
        Map<String, Object> data1 = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("XMJSHJ", -10);
        data.put("XMMC", "POLO衫");
        data.put("XMBM", "202020012");
        data.put("GGXH", "25kg/桶");
        data.put("DW", "千克");
        data.put("XMSL", 2);
        //data.put("SE", -1.38);
        //税率16%需要写成0.16的格式
        data.put("SL", 0.16);
        //SPBM字段为商品税收分类编码，不同的商品会有不同的编码，不对应的话会影响报税，需要咨询下公司财务
        data.put("SPBM", "1090625010000000000");
        data.put("FPHXZ", "1");
        data.put("HH", "1");
        items.add(data);

        data1.put("XMJSHJ", 100);
        data1.put("XMMC", "POLO衫");
        data1.put("XMBM", "202020012");
        data1.put("GGXH", "25kg/桶");
        data1.put("DW", "千克");
        data1.put("XMSL", 2);
        data1.put("SE", 13.79);
        //税率16%需要写成0.16的格式
        data1.put("SL", 0.16);
        //SPBM字段为商品税收分类编码，不同的商品会有不同的编码，不对应的话会影响报税，需要咨询下公司财务
        data1.put("SPBM", "1090625010000000000");
        data1.put("FPHXZ", "2");
        data1.put("HH", "2");
        data1.put("ZKHHH", "1");
        items.add(data1);

        data2.put("XMJSHJ", 10);
        data2.put("XMMC", "饮用水 娃哈哈 Wahaha");
        data2.put("XMBM", "1316927134524661760");
        data2.put("GGXH", "596ml");
        data2.put("DW", "箱");
        data2.put("XMSL", 2);
        // data.put("SE", -57.52);
        //税率16%需要写成0.16的格式
        data2.put("SL", 0.13);
        //SPBM字段为商品税收分类编码，不同的商品会有不同的编码，不对应的话会影响报税，需要咨询下公司财务
        data2.put("SPBM", "3060101000000000000");
        items.add(data2);
        return items;
    }


    /**
     * 获取发票请求流水号
     * 长度不超过20位，长度在1到20位的字母和数字组合，不可以重复的，不要包含window系统文件名限制的特殊字符
     *
     * @return 发票请求流水号
     */
    public static String buildFpqqlsh() {
        //return "svc20210628141012739";
        StringBuilder rs = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<20;i++){
            rs.append(random.nextInt(9));
        }
        return rs.toString();
    }


    /**
     * 发票作废Requestdatas参数
     */
    public static String buildInvalidRequestdatas() {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("fpHm", "16138377");//发票号码,必填
        data.put("fpDm", "233005345591");//发票代码,必填
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(data);
    }

    /**
     * 构造 发票红冲请求 参数
     * @param fpqqlsh
     * @return
     */
    public static Object redRequestDatas(String fpqqlsh) {
        List<Object> datas = new ArrayList<Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("FPQQLSH",fpqqlsh );
        //开票点编码 如果一个税号对应多个开票点，此字段必输，用于确定唯一开票点
        //data.put("ORGCODE", "");
        //蓝色发票代码
        data.put("fpDm", "011002680026");
        //蓝色发票号码
        data.put("fpHm", "03197960");
        //红字信息表编号 专票红冲时必填
        data.put("hzzxxbh", "");
        datas.add(data);
        GsonBuilder builder = new GsonBuilder();
        return builder.create().toJson(datas);
    }
}
